package com.masterstroke.template.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.masterstroke.template.common.constant.MessageConstant;
import com.masterstroke.template.common.context.BaseContext;
import com.masterstroke.template.common.exception.BaseException;
import com.masterstroke.template.common.utils.Encipher;
import com.masterstroke.template.mapper.OrderMapper;
import com.masterstroke.template.pojo.domain.Order;
import com.masterstroke.template.pojo.domain.Product;
import com.masterstroke.template.pojo.dto.BuyProduct;
import com.masterstroke.template.pojo.dto.InsertProductDTO;
import com.masterstroke.template.service.ProductService;
import com.masterstroke.template.mapper.ProductMapper;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

import static com.fasterxml.jackson.databind.type.LogicalType.DateTime;

/**
* @author 35925
* @description 针对表【product】的数据库操作Service实现
* @createDate 2024-12-15 16:07:47
*/
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product>
implements ProductService{
    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private OrderMapper orderMapper;

    public int insert(InsertProductDTO product){
        Product product1 = new Product();
        String proName=product.getProductName();

        QueryWrapper<Product> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("product_name",proName);
        List<Product> list =productMapper.selectList(queryWrapper);
        if(!list.isEmpty()) {
            throw new BaseException(MessageConstant.ALREADY_EXIST);
        }
        product1.setProductName(proName);
        product1.setProductPrice(product.getProductPrice());
        product1.setProductId(Encipher.getUUID());
        product1.setProductDescribe(product.getProductDescription());
        return productMapper.insert(product1);
    }

    @Override
    public void buyProduct(BuyProduct buyProduct) {
        if(BaseContext.getCurrentId()==null){
            throw new BaseException(MessageConstant.UNKNOWN_ERROR);
        }
        Order order=new Order();
        order.setOrderId(Encipher.getUUID());
        order.setProductId(buyProduct.getProductId());
        order.setProductNum(buyProduct.getProductNum());
        order.setCreateTime(LocalDateTime.now());
        order.setUserId(BaseContext.getCurrentId());
        orderMapper.insert(order);
    }
}
